﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using ENTITY;
using System.Configuration;

namespace MODELS
{
    public class dbConnect
    {
        private SqlConnection sqlConn;

        public dbConnect()
        {
            string strSQL = @"Server = (local); 
                              Database = DNMICBOOK; 
                              Integrated Security = true;
                                ";
            sqlConn = new SqlConnection(strSQL);
        }

        public DataTable GetData(string strSQL)
        {
            SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlConn);
            DataTable dt = new DataTable();
            sqlConn.Open();
            da.Fill(dt);
            sqlConn.Close();
            return dt;
        }

        public DataTable GetData(string procName, SqlParameter[] para)
        {
            SqlCommand cmd = new SqlCommand(procName, sqlConn);
            cmd.CommandType = CommandType.StoredProcedure;
            if (para != null)
            {
                cmd.Parameters.AddRange(para);
            }
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            sqlConn.Open();
            da.Fill(dt);
            sqlConn.Close();
            return dt;

        }

        public int ExecuteSQL(string strSQL)
        {
            SqlCommand cmd = new SqlCommand(strSQL, sqlConn);
            sqlConn.Open();
           int row =  cmd.ExecuteNonQuery();
            sqlConn.Close();
            return row;
        }

        public int ExecuteSQL(string ProcName, SqlParameter[] para)
        {
            SqlCommand cmd = new SqlCommand(ProcName, sqlConn);
            cmd.CommandType = CommandType.StoredProcedure;
            if (para != null)
                cmd.Parameters.AddRange(para);
            sqlConn.Open();
            int row = cmd.ExecuteNonQuery();
            sqlConn.Close();
            return row;
        }
    }
}
